<?php
namespace  app\admin\controller;

use app\admin\AdminCommon;
use think\Db;

class Orders extends AdminCommon
{
    //订单页面
    public function  getindex()
    {
        $data=Db::table('tp_orders')->alias('to')
            ->field('to.id,to.total,to.create_time,to.status,ta.address_address,ta.address_detailed,tu.user_name,tu.user_pic')
            ->join('tp_address ta','to.address_id=ta.id','left')
            ->join('tp_user tu','to.user_id=tu.id','left')
            ->select();
        $i=0;
        foreach($data as $v)
        {
            $data[$i]['user_pic']='/uploads/user_pic/'.$v['user_pic'];
            $data[$i]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            $i++;
        }
        return $this->fetch('orders/index',['data'=>$data,'b'=>1]);
    }

    public function postwait()
    {
       $resquest=$this->request;
       $res=$resquest->except('action');

       $ress=Db::table('tp_orders')->where('id',$res['id'])->value('status');
       switch($ress)
       {
           case 0:
               $result=Db::table('tp_orders')->where('id',$res['id'])->update(['status'=>'1']);
               $massage="代发货";
               break;
           case 1:
               $result=Db::table('tp_orders')->where('id',$res['id'])->update(['status'=>'2']);
               $massage="待收货";
               break;
           case 2:
               $result=Db::table('tp_orders')->where('id',$res['id'])->update(['status'=>'3']);
               $massage="已收货";
               break;
           case 3:
               $result=Db::table('tp_orders')->where('id',$res['id'])->update(['status'=>'4']);
               $massage="待评价";
               break;
           case 4:
               $result=Db::table('tp_orders')->where('id',$res['id'])->update(['status'=>'5']);
               $massage="已评价";
               break;
       }


       if($result){
            $data=
                [
                    'status'=>0,
                    'id'=>$res['id'],
                    'msg'=>$massage,
                ];
        }else{
            $data=
                [
                    'status'=>1,
                    'msg'=>'出现未知错误，请稍后重试',
                    'id'=>$res['id'],
                ];
        }
        return json($data);
    }


    public function getdetail_index($id)
    {
      $request=$this->request;
      $res=$request->param();
      $data=Db::table('tp_orders')->alias('to')
            ->field('to.id,tod.id,tod.goods_id,tod.goods_num,tg.goods_name,tg.goods_pic,tg.goods_price')
            ->join('tp_orders_detail tod','tod.orders_id=to.id','right')
            ->join('tp_goods tg','tod.goods_id=tg.id','left')
            ->where('to.id',$res['id'])
            ->select();
        $i=0;
        foreach($data as $v)
        {
            $data[$i]['goods_pic']='/uploads/goods_pic/'.$v['goods_pic'];
            $i++;
        }
       return $this->fetch('orders/orders_detail',['data'=>$data,'b'=>1]);
    }
}
?>